// this file is used to handle the slice of userId
import { PayloadAction, createSlice } from '@reduxjs/toolkit';

import { getStorageBooleanItem, setStorageItem } from '@/utils/storage';

interface appState {
  /** 是否首次启动 */
  isFirstLaunch: boolean;
  /** 是否在线 */
  online: boolean;
  /** 是否显示底部导航栏 */
  showHomeTabBar: boolean;
  /** 是否显示订阅退出按钮 */
  showSubScriptionExitButton: boolean;
}

const initialState: appState = {
  isFirstLaunch: getStorageBooleanItem('isFirstLaunch') ?? true,
  online: false,
  showHomeTabBar: true,
  showSubScriptionExitButton: false,
};

export const appSlice = createSlice({
  name: 'application',
  initialState: initialState,
  reducers: {
    // 设置是否首次启动
    setIsFirstLaunch: (state, action: PayloadAction<boolean>) => {
      setStorageItem('isFirstLaunch', action.payload);
      state.isFirstLaunch = action.payload;
    },
    // 设置在线状态
    setOnline: (state, action: PayloadAction<boolean>) => {
      state.online = action.payload;
    },
    // 设置是否显示底部导航栏
    setShowHomeTabBar: (state, action: PayloadAction<boolean>) => {
      state.showHomeTabBar = action.payload;
    },
    // 设置是否显示订阅退出按钮
    setShowSubScriptionExitButton: (state, action: PayloadAction<boolean>) => {
      state.showSubScriptionExitButton = action.payload;
    },
  },
});

export const { setOnline, setShowHomeTabBar, setIsFirstLaunch, setShowSubScriptionExitButton } = appSlice.actions;
